/**
 * @fileoverview Giant Interective Group, Inc. Javascript Library v#version.
 * 该Javascript UI库是基于jQuery的扩展。
 * <pre>
 * Copyright (C) 2004-2009 Giant Interective Group, Inc. All rights reserved.
 * 版权所有 2004-2009 上海巨人网络科技有限公司
 * </pre>
 * @version #version, #date
 * @author  Zhangkai
 * Depend on jQuery 1.3.x
 *            giant.core.js
 */
if (typeof giant === 'undefined')
    var giant = window.giant = {};
if (typeof giant.ui === 'undefined')
    giant.ui = {};
(function($) {
    var intervel = null;
    giant.ui.marquee = function(options) {
        this.opts = $.extend({}, giant.ui.marquee.defaults, options,true);
        this.$li =$("#"+this.opts.id).find("li");
        this.$inner = $("#"+this.opts.id).find(".ui-marqueeInner");
        this.total =this.$li.size();
        this.activeIndex = 0;
        this._init();
        this.isMoving = false;
    };
    giant.ui.marquee.prototype = {
         _init:function(){
            if(this.total>this.opts.number){    //当总数大于显示数时，自动播放，否则不自动播放
              $("#"+this.opts.id).find("ul:first").clone().appendTo(this.$inner);
              this.startAutoMarquee();  
            }
             this._eventBind();
         },
        _eventBind:function(){
            var $this = this;
            $("#btnLeft").bind("click",function(){
                   if(!$this.isMoving){
                       $this.clearAutoMarquee();
                        $this.scroll(-6);                       
                   }
            });
             $("#btnRight").bind("click",function(){
                   if(!$this.isMoving){
                        $this.clearAutoMarquee();
                        $this.scroll(6);
                   }
            });  
        },
        scroll:function(num){
            this.activeIndex+=num;
            var $this = this;
            this.isMoving = true;
             $("#textfield").val(this.activeIndex);
            if(this.activeIndex>=this.total+5){
                var left = parseInt(this.$inner.css("left"))+ $("#"+this.opts.id).find("ul").width();
                this.activeIndex -= this.total;
               this.$inner.css("left",left+"px");
            }
             if(this.activeIndex<0) {
                  var left = parseInt(this.$inner.css("left"))- $("#"+this.opts.id).find("ul").width();
                  this.activeIndex += this.total;
                  this.$inner.css("left",left+"px");
            }
            var animateWidth = num>=0?"-="+(num*this.opts.unitWidth)+"px":"+="+(-1*num*this.opts.unitWidth)+"px";
            var speed = 400;
            if(Math.abs(num)>1){
                speed = speed*Math.abs(num)/2;
            }
            this.$inner.animate({left:animateWidth},speed,function(){
                $this.isMoving = false;
            });
        },
        startAutoMarquee:function(){
            var $this = this;
            intervel = window.setInterval(function(){
               $this.scroll(1);
            },3000);
        },
        clearAutoMarquee:function(){
           if(intervel){
               window.clearInterval(intervel);
           }
        }
    };
    giant.ui.marquee.defaults = {
       id:"marquee",
       unitWidth:96,
       number:6,
       isAuto:true
    };
})(jQuery);